package com.psm.controller;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.psm.interceptor.IndexInterceptor;
import com.psm.model.DataGrid;
import com.psm.model.PuTerm;
/**
 * 
 * @author 广州朴石茗文化发展有限公司
 * @Email pushiming@sina.cn
 */
@Before(IndexInterceptor.class)
public class TermsController extends BaseController<PuTerm>{

	public Logger log=Logger.getLogger(getClass());

    public void index(){
        render("index.html");
    }
    public void add(){
    	String penter = this.getPara("parent");
        render("add.html");
    }
    
    /**
     * 类型列表
     */
    public void listJsonTerm(){

        List<Object> param = new ArrayList<Object>();
        StringBuffer where = new StringBuffer();
        /** 添加查询字段条件*/
        String sortName = this.getPara("sort");
        String sortOrder = this.getPara("order", "desc");

        String name = this.getPara("name", "");

        where.append(" where 1=1 ");

        if(!"".equals(name)){
            where.append(" and p.name like '%" + name + "%'");
        }

        if (sortName != null) {
            where.append(" order by ");
            where.append(sortName);
            where.append(" ");
            where.append(sortOrder);
        }
        DataGrid dg = PuTerm.me.getTerms(this.getParaToInt("page", 1), this.getParaToInt("rows", 20), where, param);

        renderJson(dg);
    }
    /**
     * 获取类型,用于下拉数据列表
     */
    public void getTermsToSelect(){
    	List<Record> list = PuTerm.me.getTermsToSelect();
    	renderJson(list);
    }
    /**
     * 获取指定类型的子项
     */
    public void getTermsForPenter(){
    	String penter = this.getPara("parent");
    	List<Record> list = PuTerm.me.getTermsForPenter(penter);
    	renderJson(list);
    }
}
